﻿namespace B13_Ex06
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using B13_Ex02;

    public partial class FormDamkaGameBoard : Form
    {
        private const int k_ButtonSize = 60;
        private readonly B13_Ex02.CheckersGameControl m_MyCheckersGame;
        private readonly FormGameProperties m_GameSettings;
        private SmartControler[,] m_GameBoard;
        private SmartControler m_FirstButtonToClick = null;
       
        public FormDamkaGameBoard()
        {
            this.m_MyCheckersGame = new CheckersGameControl(this.UiDisplayMessage, this.ChangeBoardDelegate, this.UpdatePlayersInfo, this.AnnounceWinner, this.EnableAndDisableButtons);
            this.m_GameSettings = new FormGameProperties();
            this.InitializeComponent();
            this.Show();

            this.DisplaySettingsAndStartGameAccording();
        }

        public void FindIfNewSettings()
        {
            bool canStartGame;
        
            this.StartGameSettings(out canStartGame);

            if ((this.m_GameSettings.IsChangedSizeBoard || this.m_GameSettings.IsChangedPlayerTwoState) && canStartGame)
            {
                string messageToUser = string.Format("Do you want to start new game? ");
                DialogResult dialogResult = MessageBox.Show(messageToUser, "Damka", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (dialogResult == DialogResult.Yes)
                {
                    this.m_GameSettings.IsChangedPlayerTwoState = false;
                    this.ResetAndCreateNewBoardGame();
                    this.m_MyCheckersGame.RunNewGame();
                }
                else
                {
                   if (this.m_GameSettings.IsChangedPlayerTwoState)
                   {
                       this.m_GameSettings.HasTwoPlayers = !this.m_GameSettings.HasTwoPlayers;
                   }
                }
            }
        }

        public void DisplaySettingsAndStartGameAccording()
        {
            bool canStartGame;
            this.StartGameSettings(out canStartGame);
            if (canStartGame)
            {
                this.CreateNewBoard();
                this.m_MyCheckersGame.RunNewTournament();
                this.Hide();
                this.ShowDialog();
            }  
        }

        private void ResetTheBoard()
        {
            this.ResetBoardDelegate(this.m_MyCheckersGame.CheckersGameBoard.GameBoardCurrent);
            this.ReasetButtonsInBoard();
            this.m_MyCheckersGame.ResetGameBourd();
        }

        public void CreateNewBoard()
        {
            this.m_MyCheckersGame.InitGameSpecification(this.m_GameSettings.PlayerOneName, this.m_GameSettings.PlayerTwoName, this.m_GameSettings.HasTwoPlayers, this.m_GameSettings.BoardSize);
            this.initGameBoard();
        }

        private void ResetAndCreateNewBoardGame()
        {
            this.ResetTheBoard();
            this.m_GameSettings.BoardSize = this.m_GameSettings.ChangedBoardSize;
            
            if (this.m_GameSettings.IsChangedPlayerTwoState)
            {
                this.m_GameSettings.HasTwoPlayers = !this.m_GameSettings.HasTwoPlayers;
            }

            this.m_GameSettings.IsChangedPlayerTwoState = false;
            this.CreateNewBoard();
        }

        public void EnableAndDisableButtons(PlayerInfo i_Player1, PlayerInfo i_Player2, BoardSquare[,] i_BoardSquare)
        {
            for (int i = 0; i < this.m_GameSettings.BoardSize; i++)
            {
                for (int j = 0; j < this.m_GameSettings.BoardSize; j++)
                {
                    if (i_Player1.IsActive)
                    {
                        if (i_BoardSquare[i, j].SquareState == i_Player2.Color || i_BoardSquare[i, j].SquareState == i_Player2.ColorKing)
                        {
                            this.m_GameBoard[i, j].Enabled = false;
                            this.m_GameBoard[i, j].BackColor = Color.LightGray;
                        }
                        else if (i_BoardSquare[i, j].SquareState == i_Player1.Color || i_BoardSquare[i, j].SquareState == i_Player1.ColorKing)
                        {
                            this.m_GameBoard[i, j].Enabled = true;
                            this.m_GameBoard[i, j].BackColor = Color.White;
                        }   
                    }
                    else
                    {
                        if (i_BoardSquare[i, j].SquareState == i_Player1.Color || i_BoardSquare[i, j].SquareState == i_Player1.ColorKing)
                        {
                            this.m_GameBoard[i, j].Enabled = false;
                            this.m_GameBoard[i, j].BackColor = Color.LightGray;
                        }
                        else if (i_BoardSquare[i, j].SquareState == i_Player2.Color || i_BoardSquare[i, j].SquareState == i_Player2.ColorKing)
                        {
                            this.m_GameBoard[i, j].Enabled = true;
                            this.m_GameBoard[i, j].BackColor = Color.White;
                        }
                    }

                    if (i_BoardSquare[i, j].IsValidSquare && i_BoardSquare[i, j].SquareState == i_BoardSquare[i, j].Empty)
                    {
                        this.m_GameBoard[i, j].Enabled = true;
                        this.m_GameBoard[i, j].BackColor = Color.White;
                    }
                }
            }
        }

        public void UiDisplayMessage(string i_Announcement)
        {
            MessageBox.Show(i_Announcement, "Damka", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);   
        }

        public void AnnounceWinner(PlayerInfo i_Winner, PlayerInfo i_MaybeWinner)
        {
            DialogResult dialogResult;
            string messageToUser;
            if (i_MaybeWinner == null)
            {
                messageToUser = string.Format("{0} Won! and now has: {2} wins! {1} Another round?", i_Winner.PlayerName, Environment.NewLine, i_Winner.Victories);
                dialogResult = MessageBox.Show(messageToUser, "Damka", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }
            else
            {
                messageToUser = string.Format("There is a Tie! {0} Another round?", Environment.NewLine);
                dialogResult = MessageBox.Show(messageToUser, "Damka", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }

            if (dialogResult == DialogResult.No)
            {
                this.Close();
            }
            else
            {
                this.ResetAndCreateNewBoardGame();
                this.m_MyCheckersGame.RunNewTournament();
            }
        }

        public void UpdatePlayersInfo(PlayerInfo i_Player1, PlayerInfo i_Player2)
        {
          string firstPlayer = string.Format("{0} (Yellow) : {1} ", i_Player1.PlayerName, i_Player1.Points);
          string secoundPlayer = string.Format("{0} (Red) : {1} ", i_Player2.PlayerName,  i_Player2.Points);
          this.toolStripStatusLabel1.Text = firstPlayer + secoundPlayer;
        }

        public void ChangeBoardDelegate(BoardSquare[,] i_GameBoard)
        {   
           string s = char.ToString(GamePiece.BlackPiece);
            for (int i = 0; i < this.m_GameSettings.BoardSize; i++)
            {
                for (int j = 0; j < this.m_GameSettings.BoardSize; j++)
                {
                    if(i_GameBoard[i, j].SquareState == GamePiece.BlackPiece)
                    {
                        this.m_GameBoard[i, j].BackgroundImage = Properties.Resources.CoinRed;
                    }
                    else if (i_GameBoard[i, j].SquareState == GamePiece.WhitePiece)
                    {
                        this.m_GameBoard[i, j].BackgroundImage = Properties.Resources.CoinYellow;
                    }
                    else if (i_GameBoard[i, j].SquareState == GamePiece.WhitePieceKing)
                    {
                        this.m_GameBoard[i, j].BackgroundImage = Properties.Resources.CoinYellowKing;
                    }
                    else if (i_GameBoard[i, j].SquareState == GamePiece.BlackPieceKing)
                    {
                        this.m_GameBoard[i, j].BackgroundImage = Properties.Resources.CoinRedKing;
                    } 
                    else
                    {
                        this.m_GameBoard[i, j].BackgroundImage = null;
                    }

                    this.m_GameBoard[i, j].Invalidate();
                }
            }
        }

        public void ResetBoardDelegate(BoardSquare[,] i_GameBoard)
        {
            string s = char.ToString(GamePiece.BlackPiece);
            for (int i = 0; i < this.m_GameSettings.BoardSize; i++)
            {
                for (int j = 0; j < this.m_GameSettings.BoardSize; j++)
                {
                    this.m_GameBoard[i, j].BackgroundImage = null;
                    this.m_GameBoard[i, j].Invalidate();
                }
            }
        }

        private void StartGameSettings(out bool o_CanStartGame)
        {
            o_CanStartGame = false;

            this.m_GameSettings.ShowDialog();

            if (this.m_GameSettings.DialogResult == DialogResult.OK)
            {
                o_CanStartGame = true;
            }
        }

        private void damkaGameBoard_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }

        private void startANewGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ResetAndCreateNewBoardGame();
            this.m_MyCheckersGame.RunNewGame();
        }

        private void startANewToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ResetAndCreateNewBoardGame();
            this.m_MyCheckersGame.RunNewTournament();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void properToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.FindIfNewSettings();
            this.m_MyCheckersGame.ChangePlayersName(this.m_GameSettings.PlayerOneName, this.m_GameSettings.PlayerTwoName);
        }
    }
}
